/*#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
#include <cstring>
using namespace std;
void *pthreadrun(void *arg)
{
    //pthread_detach(pthread_self());
    while (1)
    {
        cout << "我是new线程，我在运行。。。" <<endl;
        sleep(1);
        break;
    }
}
int main()
{
    pthread_t t1;
    pthread_create(&t1, nullptr, pthreadrun, nullptr);
    sleep(2);
    int n=pthread_join(t1,nullptr);
    cout<<"error node:"<<n<<"->"<<strerror(n)<<endl;
    while (1)
    {
        sleep(1);
        cout << "我是main线程，我在运行。。。" << endl;
    }

    return 0;
}*/

/*#include <thread>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
void *run1(void *arg)
{
    while (1)
    {
        cout << "我是new线程" << endl;
        sleep(5);
        break;
    }
}
int main()
{
    void* ret;
    thread t1(run1,nullptr);
    t1.join();
    while(1)
    {
        sleep(1);
        cout<<"我是main线程"<<endl;
    }
    return 0;
}

/*#include <iostream>
#include <thread>

using namespace std;

void print(int n) {
    for (int i = 0; i < n; ++i) {
        cout << this_thread::get_id() << ":" << i << endl; // 打印 [线程id]:i
        this_thread::sleep_for(chrono::seconds(1)); // sleep 1 秒
    }
}

int main() {
    thread t1(print, 100);
    thread t2(print, 100);

    t1.join();
    t2.join();

    return 0;
}*/

/*#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
int ticket=1000;
pthread_mutex_t pm;
void* run1(void* arg)
{
    int cnt=10;
    while(cnt--)
    {
        pthread_mutex_lock(&pm);
        cout<<"new 1 ："<<--ticket<<"   "<<endl;
        pthread_mutex_unlock(&pm);
        sleep(1);
    }
}
void* run2(void* arg)
{
    int cnt=10;
    while(cnt--)
    {
        pthread_mutex_lock(&pm);
        cout<<"new 2 ："<<--ticket<<"   "<<endl;
        pthread_mutex_unlock(&pm);
        sleep(1);
    }

}
int main()
{
    pthread_mutex_init(&pm,nullptr);
    pthread_t t1,t2;
    pthread_create(&t1,nullptr,run1,nullptr);
    pthread_create(&t2,nullptr,run2,nullptr);
    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    return 0;
}*/

/*#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void* run(void* arg)
{
    pthread_mutex_lock(&mutex);
    cout<<"new thread"<<endl;
    pthread_mutex_lock(&mutex);
cout<<"unlock succeed";

}
int main()
{
pthread_t t1,t2,t3;
pthread_create(&t1,nullptr,run,nullptr);
cout<<"main thread"<<endl;
sleep(3);
pthread_mutex_unlock(&mutex);
pthread_join(t1,nullptr);
   return 0;
}*/

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *run(void *arg)
{
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond, &mutex);
    cout << "cond_wait already solve" << endl;
    pthread_mutex_unlock(&mutex);
}
int main()
{
    pthread_t tids[5];
    for (int i = 0; i < 5; i++)
    {
        pthread_create(tids + i, nullptr, run, nullptr);
    }
    sleep(3);

    for (int i = 0; i < 5; i++)
    {
        sleep(1);
        pthread_cond_signal(&cond);
    }
    for (int i = 0; i < 5; i++)
        pthread_join(tids[i], nullptr);

    return 0;
}